package com.qianli.user.domain.respository;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.xjd.user.client.request.Mobile;
import com.fqgj.xjd.user.client.request.UserAccess;
import com.fqgj.xjd.user.dao.*;
import com.fqgj.xjd.user.entity.*;
import com.google.common.collect.Maps;
import com.qianli.user.domain.enums.UserModelItemStatusEnum;
import com.qianli.user.domain.model.UserBaseInfoModel;
import com.qianli.user.domain.model.base.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * name:用户基本信息
 * auth:ting.li@qianli-inc.com
 * date:2018/1/2 21:00
 * version: v1.0.0
 **/
@Repository
public class UserBaseInfoRespository implements BaseRespository<UserBaseInfoModel> {

    @Autowired
    private UserBaseDao userBaseDao;

    @Autowired
    private UserBaseContactDao userBaseContactDao;

    @Autowired
    private UserBaseResidentDao userBaseResidentDao;

    @Autowired
    private UserBaseWorkDao userBaseWorkDao;

    @Autowired
    private UserAccessDao userAccessDao;

    @Autowired
    private UserKycDocumentDao userKycDocumentDao;


    @Override
    public void load(UserBaseInfoModel model) {
        //基本信息
        UserBaseEntity baseEntity = userBaseDao.selectByUserCode(model.getUserCode());
        if (null != baseEntity) {
            UserBase userBase = new UserBase();
            BeanUtils.copyProperties(baseEntity, userBase);
            model.setUserBase(userBase);
        }
        //联系方式
        UserBaseContactEntity baseContactEntity = userBaseContactDao.selectByUserCode(model.getUserCode());
        if (null != baseContactEntity) {
            UserContact userContact = new UserContact();
            BeanUtils.copyProperties(baseContactEntity, userContact);
            model.setUserContact(userContact);
        }
        //居住信息
        UserBaseResidentEntity baseResidentEntity = userBaseResidentDao.selectByUserCode(model.getUserCode());
        if (null != baseResidentEntity) {
            UserResident userResident = new UserResident();
            BeanUtils.copyProperties(baseResidentEntity, userResident);
            model.setUserResident(userResident);
        }
        //工作信息
        UserBaseWorkEntity baseWorkEntity = userBaseWorkDao.selectByUserCode(model.getUserCode());
        if (null != baseWorkEntity) {
            UserWork userWork = new UserWork();
            BeanUtils.copyProperties(baseWorkEntity, userWork);
            model.setUserWork(userWork);
        }
        // kyc信息
        Map<String, Object> params = Maps.newConcurrentMap();
        params.put("userCode", model.getUserCode());
        List<UserKycDocumentEntity> userKycDocumentEntities = userKycDocumentDao.selectByParams(params);
        if (CollectionUtils.isNotEmpty(userKycDocumentEntities)) {
            UserKycDocument userKycDocument = new UserKycDocument();
            BeanUtils.copyProperties(userKycDocumentEntities.get(0), userKycDocument);
            model.setUserKycDocument(userKycDocument);
        }

        // userAccess
        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(model.getUserCode());
        if (null != userAccessEntity) {
            UserAccess ro = new UserAccess();
            BeanUtils.copyProperties(userAccessEntity, ro);
            ro.setMobile(new Mobile(userAccessEntity.getMobile()));
            model.setUserAccess(ro);
        }


    }

    @Override
    public void store(UserBaseInfoModel model) {
        String userCode = model.getUserCode();
        //基本信息
        if (null != model.getUserBase()) {
            UserBaseEntity baseEntity = new UserBaseEntity();
            BeanUtils.copyProperties(model.getUserBase(), baseEntity);
            if (UserModelItemStatusEnum.ADD.getType().equals(model.getUserBase().getItemStatus())) {
                userBaseDao.insert(baseEntity);
            } else {
                userBaseDao.updateByPrimaryKey(baseEntity);
            }
        }
        //联系方式
        if (null != model.getUserContact()) {
            UserBaseContactEntity baseContactEntity = new UserBaseContactEntity();
            BeanUtils.copyProperties(model.getUserContact(), baseContactEntity);
            if (UserModelItemStatusEnum.ADD.getType().equals(model.getUserContact().getItemStatus())) {
                userBaseContactDao.insert(baseContactEntity);
            } else {
                userBaseContactDao.updateByPrimaryKey(baseContactEntity);
            }
        }
        //居住信息
        if (null != model.getUserResident()) {
            UserBaseResidentEntity baseResidentEntity = new UserBaseResidentEntity();
            BeanUtils.copyProperties(model.getUserResident(), baseResidentEntity);
            if (UserModelItemStatusEnum.ADD.getType().equals(model.getUserResident().getItemStatus())) {
                userBaseResidentDao.insert(baseResidentEntity);
            } else {
                userBaseResidentDao.updateByPrimaryKey(baseResidentEntity);
            }
        }
        //工作信息
        if (null != model.getUserWork()) {
            UserBaseWorkEntity baseWorkEntity = new UserBaseWorkEntity();
            BeanUtils.copyProperties(model.getUserWork(), baseWorkEntity);
            if (UserModelItemStatusEnum.ADD.getType().equals(model.getUserWork().getItemStatus())) {
                userBaseWorkDao.insert(baseWorkEntity);
            } else {
                userBaseWorkDao.updateByPrimaryKey(baseWorkEntity);
            }
        }
        // KYC
        if (null != model.getUserKycDocument()) {
            UserKycDocumentEntity kycDocumentEntity = new UserKycDocumentEntity();
            BeanUtils.copyProperties(model.getUserKycDocument(), kycDocumentEntity);
            if (UserModelItemStatusEnum.ADD.getType().equals(model.getUserKycDocument().getItemStatus())) {
                userKycDocumentDao.insert(kycDocumentEntity);
            } else {
                userKycDocumentDao.updateByPrimaryKey(kycDocumentEntity);
            }
        }
    }

    @Override
    public void restore(UserBaseInfoModel model) {

    }
}
